﻿using EasyERP_BLL.Wares;
using EasyERP_BLL;
using EasyERP_Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using EasyERP_View.POP;

namespace EasyERP_View.FrmWares
{
    /// <summary>
    /// 往来单价窗体
    /// </summary>
    public partial class FrmItemPrice : DockContent
    {
        /// <summary>
        /// 往来单价数据访问对象
        /// </summary>
        private readonly B_Item_Price_BLL Item_Price_BLL = new B_Item_Price_BLL();

        /// <summary>
        /// 存储DataGridView单元格中旧的数据
        /// </summary>
        private string str_old;

        /// <summary>
        /// 存储DataGridView单元格中新的的数据
        /// </summary>
        private string str_new;

        /// <summary>
        /// 静态子代码编号
        /// </summary>
        public static string minor_cd;

        /// <summary>
        /// 静态子代码名称
        /// </summary>
        public static string minor_nm;

        #region 构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrmItemPrice()
        {
            InitializeComponent();
        }
        #endregion

        #region 查询方法
        /// <summary>
        /// 查询方法
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private void Query()
        {
            if (string.IsNullOrEmpty(TxtBp_cd.Text.Trim()))
            {
                B_Message_BLL.MessageCaveat("2003");                //弹出提示框
                return;
            }

            //数据封装
            B_Item_Price item_Price = new B_Item_Price
            {
                bp_cd = TxtBp_cd.Text.Trim()
            };
            B_Item item = new B_Item
            {
                item_nm = TxtItem_nm.Text.Trim(),
                item_group = TxtItem_Group.Text.Trim()
            };

            DataTable dt = Item_Price_BLL.Get_B_Item_Price(item_Price, item);

            //绑定DataGridView
            DGView.DataSource = dt;

            //将主代码编号列设置为只读
            foreach (DataGridViewRow row in DGView.Rows)
            {
                row.Cells[item_cd.Name].ReadOnly = true;
            }

            if (dt.Rows.Count == 0)
            {
                B_Message_BLL.MessageCaveat("1004");
            }
        }
        #endregion

        #region 判断DataGridView中是否有尚未保存的数据方法
        /// <summary>
        /// 判断DataGridView中是否有尚未保存的数据方法
        /// </summary>
        /// <returns>返回True：有未保存的数据；返回False：没有未保存的数据</returns>
        private bool BeforSave()
        {
            foreach (DataGridViewRow row in DGView.Rows)
            {
                if (row.Cells["idu"].ToolTipText != "")
                {
                    return true;
                }
            }
            return false;
        }
        #endregion

        #region 单击查询按钮事件
        /// <summary>
        /// 单击查询按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSearch_Click(object sender, EventArgs e)
        {
            //判断是否有未保存数据
            if (BeforSave())
            {
                DialogResult result = B_Message_BLL.MessageYesOrNo("2100");
                if (result == DialogResult.Yes)
                {
                    Query();
                }
            }
            else
            {
                Query();
            }
        }
        #endregion

        #region 单击添加行按钮事件
        /// <summary>
        /// 单击添加行按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnAddRow_Click(object sender, EventArgs e)
        {
            //判断DataGridView是否为空
            if (DGView.DataSource == null)
            {
                DGView.Rows.Add();
            }
            else
            {
                ((DataTable)DGView.DataSource).Rows.Add();
            }

            //修改行首图标
            DGView.Rows[DGView.Rows.Count - 1].Cells["idu"].Value = Properties.Resources.I;
            DGView.Rows[DGView.Rows.Count - 1].Cells["idu"].ToolTipText = "I";
        }
        #endregion

        #region 单击删除行按钮事件
        /// <summary>
        /// 单击删除行按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnDelRow_Click(object sender, EventArgs e)
        {
            //遍历所有选中的行
            foreach (DataGridViewRow row in DGView.SelectedRows)
            {
                //判断该行是否是新添加的行
                if (row.Cells["idu"].ToolTipText == "I")
                {
                    DGView.Rows.Remove(row);    //删除行
                }
                else//不是新添加的行
                {
                    //替换行首图标为删除图标
                    row.Cells["idu"].Value = Properties.Resources.D;
                    //修改鼠标悬停的提示符
                    row.Cells["idu"].ToolTipText = "D";
                }
            }
        }
        #endregion

        #region 单击单元格按钮事件
        /// <summary>
        /// 单击单元格按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DGView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                if (DGView.Columns[e.ColumnIndex].Name == Btn_item.Name)        //单位按钮
                {
                    //B_Minor model = new B_Minor();
                    //model.major_cd = "1003";
                    //FrmPopMinor popMinor = new FrmPopMinor(model);
                    //popMinor.ShowDialog();

                    //if (!string.IsNullOrEmpty(minor_cd) && !string.IsNullOrEmpty(minor_nm))
                    //{
                    //    DGView[item_unit.Name, e.RowIndex].Value = minor_cd;
                    //    DGView[item_unit_nm.Name, e.RowIndex].Value = minor_nm;
                    //}
                }

                //修改idu栏的图标
                if (!string.IsNullOrEmpty(minor_cd) && !string.IsNullOrEmpty(minor_nm))
                {
                    if (DGView.Rows[e.RowIndex].Cells["idu"].ToolTipText == "")
                    {
                        //修改行首图标
                        DGView.Rows[e.RowIndex].Cells["idu"].Value = Properties.Resources.U;
                        DGView.Rows[e.RowIndex].Cells["idu"].ToolTipText = "U";
                    }
                }
                //清空数据
                minor_cd = "";
                minor_nm = "";
            }
        }
        #endregion

        #region 当前选中单元格的编辑模式启动时事件
        /// <summary>
        /// 当前选中单元格的编辑模式启动时事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DGView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            str_old = string.Empty;     //清空数据
            //获取当前表格编辑前的数据
            str_old = DGView[e.ColumnIndex, e.RowIndex].Value == null ? "" : DGView[e.ColumnIndex, e.RowIndex].Value.ToString();
        }
        #endregion

        #region 当前选中单元格的编辑模式停止时事件
        /// <summary>
        /// 当前选中单元格的编辑模式停止时事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DGView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            str_new = string.Empty;     //清空数据
            //获取当前表格编辑结束的数据
            str_new = DGView[e.ColumnIndex, e.RowIndex].Value == null ? "" : DGView[e.ColumnIndex, e.RowIndex].Value.ToString();
            //判断编辑前的数据和编辑后的数据是否相同，并且是查询到的数据
            if (str_old != str_new && DGView.Rows[e.RowIndex].Cells["idu"].ToolTipText == "")
            {
                //修改行首图标
                DGView.Rows[e.RowIndex].Cells["idu"].Value = Properties.Resources.U;
                DGView.Rows[e.RowIndex].Cells["idu"].ToolTipText = "U";
            }
        }
        #endregion

        #region 单击保存按钮事件
        /// <summary>
        /// 单击保存按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnSave_Click(object sender, EventArgs e)
        {
            //声明主代码实体对象
            B_Item_Price model = new B_Item_Price();
            foreach (DataGridViewRow row in DGView.Rows)
            {
                model.bp_cd = row.Cells[bp_cd.Name].Value == null ? "" : row.Cells[bp_cd.Name].Value.ToString();
                model.item_cd = row.Cells[item_cd.Name].Value == null ? "" : row.Cells[item_cd.Name].Value.ToString();
                if (row.Cells[item_price.Name].Value == null || row.Cells[item_price.Name].Value.ToString() == "")
                {
                    model.item_price = null;
                }
                else
                {
                    model.item_price = Convert.ToDecimal(row.Cells[item_price.Name].Value.ToString());
                }
                model.remark = row.Cells[remark.Name].Value == null ? "" : row.Cells[remark.Name].Value.ToString();
                model.user_cd = FrmMain.user_cd;

                //判断每行需要调用的方法：增删改
                if (row.Cells["idu"].ToolTipText == "")      //没有修改
                {
                    continue;
                }
                else if (row.Cells["idu"].ToolTipText.ToUpper() == "I")     //新增
                {
                    if (!Item_Price_BLL.Insert_B_Item_Price(model))
                    {
                        return;
                    }
                }
                else if (row.Cells["idu"].ToolTipText.ToUpper() == "D")     //删除
                {
                    if (!Item_Price_BLL.Delete_B_Item_Price(model))
                    {
                        return;
                    }
                }
                else if (row.Cells["idu"].ToolTipText.ToUpper() == "U")     //修改
                {
                    if (!Item_Price_BLL.Update_B_Item_Price(model))
                    {
                        return;
                    }
                }
                row.Cells["idu"].Value = Properties.Resources.Q;
                row.Cells["idu"].ToolTipText = "";
            }
            Query();
            B_Message_BLL.MessageCaveat("1005");
        }
        #endregion

        #region 当关闭窗体时事件
        /// <summary>
        /// 当关闭窗体时事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmItemPrice_FormClosing(object sender, FormClosingEventArgs e)
        {
            //判断是否是用户关闭
            if (e.CloseReason == CloseReason.UserClosing)
            {
                //判断是否有未保存的数据
                if (BeforSave())        //有未保存数据
                {
                    DialogResult result = B_Message_BLL.MessageYesOrNo("2100");     //弹出提示框
                    if (result == DialogResult.Yes)
                    {
                        e.Cancel = false;       //关闭窗体
                    }
                    else
                    {
                        e.Cancel = true;        //不关闭窗体
                    }
                }
            }
        }
        #endregion

        #region 单击商品分类按钮事件
        /// <summary>
        /// 单击商品分类按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnClearItem_Group_Click(object sender, EventArgs e)
        {
            B_Minor model = new B_Minor();
            model.major_cd = "1004";
            FrmPopMinor popMinor = new FrmPopMinor(model);
            popMinor.ShowDialog();

            if (!string.IsNullOrEmpty(minor_cd) && !string.IsNullOrEmpty(minor_nm))
            {
                TxtItem_Group.Text = minor_cd;
                LblItem_Group_nm.Text = minor_nm;
            }
            //清空数据
            minor_cd = "";
            minor_nm = "";
        }
        #endregion
    }
}
